package com.tfkj.repository;

import com.tfkj.entity.Image;
import com.tfkj.entity.QuestionType;
import com.tfkj.vo.ImageWithGradeAndSubjectVO;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface ImageRepository {
    @Select("""
        select a.*,b.grade,b.subject 
        from schooltest.image a
        left join schooltest.test b
        on a.testId = b.id
        where testId = ${testId}    
    
    """)
    List<ImageWithGradeAndSubjectVO> findAllByTestId(@Param("testId")Integer testId);

    @Insert("insert into schooltest.image(testId, url , sortNum, pageNum, position, width, height, resultPosition, questionModel, score, grade, subject) values(${testId}, #{url}, ${sortNum}, ${pageNum}, #{position}, ${width}, ${height}, #{resultPosition}, #{questionModel}, ${score}, #{grade}, #{subject})")
    Integer insertImage(@Param("testId") Integer testId, @Param("url") String url, @Param("sortNum") Integer sortNum, @Param("pageNum")Integer pageNum, @Param("position") String position, @Param("width") Double width, @Param("height") Double height, @Param("resultPosition") String resultPosition, @Param("questionModel") String questionModel, @Param("score") Integer score, @Param("grade") String grade, @Param("subject") String subject);

    @Select("select MAX(sortNum) from schooltest.image where testId = ${testId}")
    Integer findMaxSortByTestId(Integer testId);

    @Select("select url from schooltest.image where id in ${ids}")
    List<String> findUrlsById(@Param("ids") String ids);

    @Insert("insert into schooltest.image(testId, url , sortNum, pageNum, position, width, height, resultPosition, questionModel, grade, subject) values(${testId}, #{url}, ${sortNum}, ${pageNum}, #{position}, ${width}, ${height}, #{resultPosition}, #{questionModel}, #{grade}, #{subject})")
    Integer insertImageNew(@Param("testId") Integer testId, @Param("url") String url, @Param("sortNum") Integer sortNum, @Param("pageNum")Integer pageNum, @Param("position") String position, @Param("width") Double width, @Param("height") Double height, @Param("resultPosition") String resultPosition, @Param("questionModel") String questionModel, @Param("grade") String grade, @Param("subject") String subject);

    @Update("<script>" +
            " UPDATE schooltest.image " +
            "<set>" +
            "  <if test='grade != null'>grade = #{grade},</if>" +
            "  <if test='subject != null'>subject = #{subject},</if>" +
            "  <if test='questionType != null'>questionType = #{questionType},</if>" +
            "  <if test='difficulty != null'>difficulty = #{difficulty},</if>" +
            "  <if test='result != null'>`result` = #{result},</if>" +
            "</set>" +
            " WHERE id = #{id}" +
            "</script>")
    Integer updateImage(Integer id, String grade, String subject, String questionType, Integer difficulty, String result);

    @Select("SELECT DISTINCT grade, subject, questionType FROM schooltest.image WHERE id IN ${ids}")
    List<QuestionType> findQuestionTypeById(@Param("ids") String ids);

    @Select("<script>" +
            " SELECT url FROM schooltest.image " +
            " WHERE grade = #{questionType.grade} " +
            " AND subject = #{questionType.subject} " +
            " AND questionType = #{questionType.questionType} " +
            " ORDER BY RAND() LIMIT ${suggestionNum}" +
            " </script>")
    List<String> findUrlsByIdWithSuggestion(@Param("questionType") QuestionType questionType, @Param("suggestionNum") Integer suggestionNum);
    @Update("<script>" +
            " UPDATE schooltest.image " +
            "<set>" +
            "  <if test='grade != null'>grade = #{grade},</if>" +
            "  <if test='subject != null'>subject = #{subject},</if>" +
            "  <if test='questionType != null'>questionType = #{questionType},</if>" +
            "  <if test='difficulty != null'>difficulty = ${difficulty},</if>" +
            "  <if test='result != null'>`result` = #{result},</if>" +
            "  <if test='testType != null'>testType = #{testType},</if>" +
            "  <if test='pageNum != null'>pageNum = ${pageNum},</if>" +
            "  <if test='sortNum != null'>sortNum = ${sortNum},</if>" +
            "</set>" +
            " WHERE id = #{id}" +
            "</script>")
    Integer updateImageWithTestType(Integer id, String grade, String subject, String questionType, Integer difficulty, String result, String testType, String pageNum, String sortNum);
}
